package com.imooc.controller;

import com.imooc.dto.OrderDto;
import com.imooc.enums.ResultEnum;
import com.imooc.exception.SellException;
import com.imooc.service.OrderService;
import com.lly835.bestpay.rest.type.Get;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * 功能描述: <br>
 * (卖家订单处理)
 *
 * @Author:Mr. xyb
 * @Date: 2018/5/18 17:33
 * @since: 1.8.0
 * @version: 1.0.0
 */
@RestController
@Slf4j
@RequestMapping("/seller/order")
public class SellerOrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 查询订单列表
     *
     * @param page 页码
     * @param size 每页数量
     * @param map  返回参数
     * @return
     */
    @RequestMapping("/list")
    public ModelAndView list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "size", defaultValue = "1") Integer size,
                             Map<String, Object> map) {
        /*设置分页参数*/
        PageRequest request = new PageRequest(page - 1, size);
        /*分页查询数据*/
        Page<OrderDto> list = orderService.findList(request);
        map.put("orderDTOPage", list);
        map.put("currentPage", page);// 当前页数
        map.put("size", size);// 每页数据量
        // System.out.println(list.getContent());
        return new ModelAndView("order/list", map);
    }

    /**
     * 取消订单
     *
     * @param orderId 订单id
     * @param map     返回参数
     * @return
     */
    @GetMapping("/cancel")
    public ModelAndView cancel(@RequestParam("orderId") String orderId, Map<String, Object> map) {
        try {
            // 查询订单
            OrderDto orderDto = orderService.findOne(orderId);
            if (orderDto == null) {
                log.error("【卖家端取消订单】 订单不存在 orderId={}", orderId);
                throw new SellException(ResultEnum.ORDER_NOT_EXIST);
            }
            // 取消订单
            orderService.cancel(orderDto);
        } catch (Exception e) {
            log.error("【卖家端取消订单】 发生异常{}", e);
            map.put("msg", e.getMessage());
            map.put("url", "/sell/seller/order/list");
            return new ModelAndView("common/error", map);
        }
        map.put("msg", ResultEnum.ORDER_CANCEL_SUCCESS.getMessage());
        map.put("url", "/sell/seller/order/list");
        return new ModelAndView("common/success");
    }

    /**
     * 查询订单详情
     *
     * @param orderId 订单id
     * @param map     返回参数
     * @return
     */
    @GetMapping("/detail")
    public ModelAndView detail(@RequestParam("orderId") String orderId, Map<String, Object> map) {
        OrderDto orderDto = new OrderDto();
        try {
            // 查询订单
            orderDto = orderService.findOne(orderId);
        } catch (Exception e) {
            log.error("【卖家端查询订单详情】 发生异常{}", e);
            map.put("msg", e.getMessage());
            map.put("url", "/sell/seller/order/list");
            return new ModelAndView("common/error", map);
        }
        map.put("orderDTO", orderDto);
        return new ModelAndView("order/detail", map);
    }

    /**
     * 完结订单
     *
     * @param orderId 订单id
     * @param map     返回参数
     * @return
     */
    @GetMapping("/finish")
    public ModelAndView finish(@RequestParam("orderId") String orderId, Map<String, Object> map) {
        try {
            // 查询订单
            OrderDto orderDto = orderService.findOne(orderId);
            // 完结订单
            orderService.finish(orderDto);
        } catch (Exception e) {
            log.error("【卖家端完结订单】 发生异常{}", e);
            map.put("msg", e.getMessage());
            map.put("url", "/sell/seller/order/list");
            return new ModelAndView("common/error", map);
        }
        map.put("msg", ResultEnum.ORDER_FINISH_SUCCESS.getMessage());
        map.put("url", "/sell/seller/order/list");
        return new ModelAndView("common/success");
    }

}
